package filecomparison;


import java.io.*;
import java.util.ArrayList;
import java.util.List;

public class Main {

    public static void main(String[] args) {

        List<FileEntity> fileEntityList = read("/Users/zzy/zh.txt");
        List<FileEntity> fileEntityList2 = read("/Users/zzy/en.txt");
        if (fileEntityList != null && fileEntityList2 != null) {
            fileEntityList.removeAll(fileEntityList2);

            try {
                write(fileEntityList, "/Users/zzy/r.txt");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    private static List<FileEntity> read(String filePath) {
        File f = new File(filePath);
        if (!f.exists()) {
            return null;
        }

        List<FileEntity> fileEntityList = new ArrayList<>();
        BufferedReader reader = null;
        try {
            reader = new BufferedReader(new InputStreamReader(new FileInputStream(f)));
            String line;
            int lineNum = 0;
            FileEntity entity = null;
            StringBuilder sb = null;
            while ((line = reader.readLine()) != null) {
                lineNum++;
                if (line.contains("=")) {
                    if (entity != null && sb.toString().trim().length() > 0) {
                        entity.value = sb.toString();
                        fileEntityList.add(entity);
                    }
                    String key = line.split("=")[0];
                    sb = new StringBuilder();
                    if (line.split("=").length > 1) {
                        sb.append(line.split("=")[1]);
                    }
                    entity = new FileEntity(lineNum, key.trim());
                }else{
                    sb.append(line);
                }
            }

            if (entity != null && sb.toString().trim().length() > 0) {
                entity.value = sb.toString();
                fileEntityList.add(entity);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (reader != null) {
                    reader.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return fileEntityList;
    }

    private static void write(List<FileEntity> entityList, String toFilePath) throws IOException {
        if (entityList == null || entityList.isEmpty()) {
            return;
        }

        File file = new File(toFilePath);
        if (file.exists()) {
            file.delete();
        }
        file.createNewFile();


        int totalLineNum = 1;
        FileWriter fw = new FileWriter(file, true);
        for (FileEntity fileEntity : entityList) {
            while (totalLineNum < fileEntity.lineNumber) {
                fw.write("\n");
                totalLineNum++;
            }
            fw.write(fileEntity.key + "=" + fileEntity.value + "\n");
            totalLineNum++;
        }
        fw.close();
    }


}
